//
//  Huati_guangchang_vc.m
//  ainkjj
//
//  Created by MAX on 2020/4/20.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Huati_guangchang_vc.h"
#import "Huati_guangchang_tvCell.h"
#import "Topic_getTopicType.h"
#import "Huati_guangchang_xiangqing_vc.h"
@interface Huati_guangchang_vc ()

<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)int refreshIndex;
@property (nonatomic, assign)NSInteger totalPage;
@end

@implementation Huati_guangchang_vc


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.groupTableViewBackgroundColor;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.layer.cornerRadius = 5;
    self.tableView.layer.masksToBounds = YES;
    
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    // tableView.separatorColor = UIColor.redColor;
    self.tableView.rowHeight = 92;
    //[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    self.tableView.mj_header=[MJRefreshNormalHeader  headerWithRefreshingTarget:self refreshingAction:@selector(req)];
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView registerNib:[UINib nibWithNibName:@"Huati_guangchang_tvCell" bundle:nil] forCellReuseIdentifier:@"Huati_guangchang_tvCell"];
    
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    // y: Lzy_StatusBarAndNavigationBarHeight +
    self.tableView.frame = CGRectMake( 16, 16, ScreenWidth - 16 * 2, self.tableView.frame.size.height - 16);
    
}
- (NSMutableArray *)dataArr{
    if(_dataArr == nil)
    {
        _dataArr = [[NSMutableArray alloc] init];
        
    }
    
    return _dataArr;
}


- (void)req{
    
    
    
    //  请求服务器，获取列表，刷新tv
    self.refreshIndex = 1;
    [self.tableView.mj_footer resetNoMoreData];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:app_code forKey:@"appCode"];
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"token"];
    
    [PPHTTPRequest req:@"java/app/topic/getTopicType" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            Topic_getTopicType *model = [Topic_getTopicType mj_objectWithKeyValues:response];
            
            if (!model.data || model.data.count == 0) {
                [self showEmptyViewWithImage:UIImageMake(@"notime_img") text:nil detailText:nil buttonTitle:nil buttonAction:NULL];
            }else{
                [self hideEmptyView];
                self.dataArr = model.data.mutableCopy;
                for (NSInteger i = 0; i < self.dataArr.count; i++) {
                    Topic_getTopicTypeData *m = self.dataArr[i];
                    m.paiming = i + 1;
                }
//                self.totalPage = model.pages;
            }
            
            
            
            
        }
        else{
            
            
            [QMUITips showError:response[@"error_msg"] ];
            
        }
        [self endMjRefresh];
        
    } failure:^(NSError *error) {
        [self endMjRefresh];
        
        [QMUITips showError:[error localizedDescription] ];
        
    }];
    
    
}
- (void)loadMoreData{
    self.refreshIndex++;
    
    if (self.refreshIndex > self.totalPage) {
        // 拿到当前的上拉刷新控件，变为没有更多数据的状态
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
    }else{
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:app_code forKey:@"appCode"];
        [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"token"];
        [dict setObject:@(self.refreshIndex).stringValue forKey:@"page"];
        [PPHTTPRequest req:@"java/app/topic/getTopicType" parameters:dict success:^(id response) {
            NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
            if ([error_code isEqualToString:@"0"]) {
                Topic_getTopicType *model = [Topic_getTopicType mj_objectWithKeyValues:response];
                
                if (!model.data) {
                    [self showEmptyViewWithImage:UIImageMake(@"notime_img") text:nil detailText:nil buttonTitle:nil buttonAction:NULL];
                }else{
                    [self hideEmptyView];
                    [self.dataArr addObjectsFromArray:model.data];
                    for (NSInteger i = 0; i < self.dataArr.count; i++) {
                        Topic_getTopicTypeData *m = self.dataArr[i];
                        m.paiming = i + 1;
                    }
                }
                
                
                
                
            }
            else{
                
                
                [QMUITips showError:response[@"error_msg"] ];
                
            }
            [self endMjRefresh];
            
        } failure:^(NSError *error) {
            [self endMjRefresh];
            
            [QMUITips showError:[error localizedDescription] ];
            
        }];
        
    }
    
}
- (void)endMjRefresh{
    
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        [self.tableView reloadData];
    });
    
    if (self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer endRefreshing];
    }
    
    
    if (self.tableView.mj_header.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
    }
    
    
}
#pragma mark - ================== table view data source delegate ==================
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Huati_guangchang_tvCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Huati_guangchang_tvCell"];
    if (!cell) {
        cell = [[Huati_guangchang_tvCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Huati_guangchang_tvCell"];
    }
    Topic_getTopicTypeData *model = self.dataArr[indexPath.row];
    
    //        cell.textLabel.text = list.title;
    //    LzyChangeDetail *m = self.dataArr[ind    exPath.row];
    //    cell.model = m;
    cell.model = model;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    Huati_guangchang_xiangqing_vc *vc = [Huati_guangchang_xiangqing_vc new];
    //  列表进入详情，需要列表带一个标志到下一页，然后在下一页请求服务器获取需要的数据
    Topic_getTopicTypeData *model = self.dataArr[indexPath.row];
    vc.model = model;
    
    vc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:vc animated:YES];

}



@end
